/* 
 * File:   Rule.cpp
 * Author: itu
 * 
 * Created on November 26, 2011, 9:36 PM
 */

#include "Rule.h"

Rule::Rule(){
    
}

Rule::Rule(Symbol *aSymbol) {
    //cout<<"New rule created."<<endl;
    setNonterminal(aSymbol);
}

void Rule::addSymbol(Symbol *newSymbol) {
    symbols.push_back(newSymbol);
}

vector<Symbol*> Rule::getSymbols() {
    return symbols;
}

void Rule::printSymbols() {
    //cout<<number<<" ";
    cout<<nonterminal->getIdentifier()<<" -> ";
    vector<Symbol*>::iterator it;
    for (it = symbols.begin(); it != symbols.end(); ++it) {
        cout << (*it)->getIdentifier() <<" ";
    }
}

string Rule::getRuleString() {
    //cout<<number<<" ";
    string out;
    out.append(nonterminal->getIdentifier());
    out.append(" -> ");
    vector<Symbol*>::iterator it;
    for (it = symbols.begin(); it != symbols.end(); ++it) {
        out.append((*it)->getIdentifier());
        out.append(" ");
    }
    return out;
}

bool Rule::hasSymbol(Symbol *symbol){
    bool alreadyInRule = false;
    for(int i=0; i<symbols.size(); i++){
        if(symbols[i]->getIdentifier().compare(symbol->getIdentifier())==0){
            alreadyInRule = true;
            break;
        }
    }
    return alreadyInRule;
}

void Rule::setNonterminal(Symbol *aSymbol){
    nonterminal = aSymbol;
}

Symbol* Rule::getNonterminal(){
    return nonterminal;
}

void Rule::setNumber(int num){
    number = num;
}

int Rule::getNumber(){
    return number;
}
